package com.example.greendaodemo;

import android.Manifest;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.greendaodemo.bean.WorkLogBean;
import com.example.greendaodemo.dao.DbUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
    private static final int REQUEST_CODE_QRCODE_PERMISSIONS = 1001;
    EditText etTime;
    EditText etDescription;
    EditText etQueryTime;
    TextView tvShowContent;
    private Button btn_save, btn_get, btn_get_all;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        askPermission();
        initViews();
        initListener();
    }

    private void initViews() {
        etTime = findViewById(R.id.et_time);
        etDescription = findViewById(R.id.et_description);
        etQueryTime = findViewById(R.id.et_query_time);
        tvShowContent = findViewById(R.id.tv_show_content);
        btn_save = findViewById(R.id.btn_save);
        btn_get = findViewById(R.id.btn_get);
        btn_get_all = findViewById(R.id.btn_get_all);
    }

    @AfterPermissionGranted(REQUEST_CODE_QRCODE_PERMISSIONS)
    private void askPermission() {
        String[] perms = {Manifest.permission.WRITE_SETTINGS, Manifest.permission.READ_EXTERNAL_STORAGE};
        if (!EasyPermissions.hasPermissions(this, perms)) {
            EasyPermissions.requestPermissions(this, "保存文件需要读写权限", REQUEST_CODE_QRCODE_PERMISSIONS, perms);
        }
    }

    private void initListener() {
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String time = etTime.getText().toString().trim();
                long timeLong = parseStrToLong(time);
                String description = etDescription.getText().toString().trim();
                WorkLogBean workLogBean = new WorkLogBean();
                workLogBean.setTimeStr(time);
                workLogBean.setTimeLong(timeLong);
                workLogBean.setDescription(description);
                DbUtils.getInstance().saveData(workLogBean);
            }
        });
        btn_get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String quertTime = etQueryTime.getText().toString().toString();
                List<WorkLogBean> datas = DbUtils.getInstance().queryDataFromTime(quertTime);
                tvShowContent.setText(datas == null ? "查询数据为空" : datas.toString());
            }
        });
        btn_get_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<WorkLogBean> list = DbUtils.getInstance().queryAllData();
                tvShowContent.setText(list == null ? "查询数据为空" : list.toString());
            }
        });
    }

    /**
     * @param timeStr
     * @return
     */
    private long parseStrToLong(String timeStr) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return simpleDateFormat.parse(timeStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    private String parseLongToStr(Long timeLong) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return simpleDateFormat.format(new Date(timeLong));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {

    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {

    }
}